package com.misboot.document.listener;


import com.misboot.document.client.SystemClient;
import com.misboot.document.constants.RequestLogType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * 开始事件监听器
 * 特殊需求
 *
 * @Author 湖南佐佑时代科技有限公司
 * @Version 5.5.0
 * @Email service@zuoyo.com
 * @Date 2023-07-17 17:13:06
 */
@Component
public class StartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
    static Logger logger = LoggerFactory.getLogger(StartedEventListener.class);
    private static final String PROJECT_NAME = "文档中心";
    private static final String PROJECT_START_STATUS = "启动成功";

    @Autowired
    SystemClient systemClient;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        logger.info("开始事件监听器");
        try {
            //处理nacos启动的时候也会执行ApplicationStartedEvent
            if (event.getApplicationContext().getDisplayName().contains("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext")) {
                InetAddress localHost = InetAddress.getLocalHost();
                String ipAddress = localHost.getHostAddress();
                Map<String, Object> map = new HashMap<>();
                map.put("operationType", RequestLogType.SERVER_SERVER_NAME);
                map.put("operationModular", PROJECT_NAME);
                map.put("operationIp", ipAddress);
                map.put("operationContent", PROJECT_START_STATUS);
                //此接口需要放开没有写带token
                systemClient.saveapi(map);
            }
        } catch (Exception e) {
            logger.error("应用启动完成,记录启动日志失败{}", e.getMessage());
        }
    }
}